package sis.ra.yahooNewsSummary;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import sis.ra.utility.Utils;


public class evaluationPrecisionRecallDCG {

	public static HashMap <String, Double> getGroundTruth()
	{
		HashMap <String, Double> gth=new HashMap <String, Double> ();
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\cnet\\groundtruth.gth";
		String content=Utils.readFile(filepath);
//		System.out.println(content);
		String [] lines=content.split("\n");
		for (int i=0;i<lines.length;i++)
		{
			String term=lines[i].split("\t")[0]+"\t"+lines[i].split("\t")[1]+"\t"+lines[i].split("\t")[2];
			Double score=Double.parseDouble(lines[i].split("\t")[3]);
	//		System.out.println(term+"=="+score);
			gth.put(term, score);
		}
		return gth;
	}
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\cnet\\";//Amazon.com_wiki.score1";
		String evaluatefile=".score14";//0
		HashMap <String, Double> gth= getGroundTruth();
		ArrayList<String> ranks=new ArrayList<String> ();
		File filepaths=new File(filepath);
		File[] files=filepaths.listFiles();
		/*
		 * overall data statistic
		 * allp5 mean the sum up score for all companies precision at 5
		 * allr5 mean the sum up score for all companies recall at 5
		 * avgp5 mean the average score for all companies precision at 5
		 * avgr5 mean the average score for all companies recall at 5
		*/
		
		Double allp5=0.0; Double allp1=0.0; Double allp2=0.0; Double allp3=0.0;Double allp4=0.0; 
		Double avgp5=0.0; Double avgp1=0.0; Double avgp2=0.0; Double avgp3=0.0;Double avgp4=0.0;
		Double allr5=0.0; Double allr1=0.0; Double allr2=0.0; Double allr3=0.0;Double allr4=0.0;
		Double avgr5=0.0; Double avgr1=0.0; Double avgr2=0.0; Double avgr3=0.0;Double avgr4=0.0;
		Double alldcg5=0.0; Double alldcg1=0.0; Double alldcg2=0.0; Double alldcg3=0.0;Double alldcg4=0.0;
		Double avgdcg5=0.0; Double avgdcg1=0.0; Double avgdcg2=0.0; Double avgdcg3=0.0;Double avgdcg4=0.0;
		HashMap <String, Integer> companyallproducts=new HashMap <String, Integer> ();
		Iterator <String> it = gth.keySet().iterator();
		while (it.hasNext())
		{
			String line=it.next();
//			System.out.println(line);
			String companyname=line.split("\t")[0];
//			System.out.println(companyname);
			if (companyallproducts.containsKey(companyname))
			{
				Integer num=companyallproducts.get(companyname);
				num=num+1;
				companyallproducts.remove(companyname);
				companyallproducts.put(companyname, num);
			}
			else
			{
				companyallproducts.put(companyname, 1);
			}
		}
		for (int t=0;t<files.length;t++)
		{
			boolean old=true;
			if (!files[t].toString().contains(evaluatefile)) continue;
	//		System.out.println(files[t].toString());
			if (files[t].toString().contains("cnet1.")||
					files[t].toString().contains("cnet11.")||
					files[t].toString().contains("cnet21.")||
					files[t].toString().contains("cnet51.")||
					files[t].toString().contains("cnet70.")||
					files[t].toString().contains("cnet61.")||
					files[t].toString().contains("cnet101.")||
					files[t].toString().contains("cnet102.")||
					files[t].toString().contains("cnet2.")||
					files[t].toString().contains("cnet22.")||
					files[t].toString().contains("cnet32.")||
					files[t].toString().contains("cnet42.")||
					files[t].toString().contains("cnet52.")||
					files[t].toString().contains("cnet62.")||
					files[t].toString().contains("cnet72.")||
					files[t].toString().contains("cnet82.")||
					files[t].toString().contains("cnet92.")||
					files[t].toString().contains("cnet71.")||
					files[t].toString().contains("cnet93.")||
					files[t].toString().contains("cnet8.")){
			
			String content=Utils.readFile(files[t].toString());
	//		System.out.println(content);
			
			String [] lines=content.split("\n");
			Double p5=0.0;Double p1=0.0;Double p2=0.0;Double p3=0.0;Double p4=0.0;
			Double dcg5=0.0;Double dcg1=0.0;Double dcg2=0.0;Double dcg3=0.0;Double dcg4=0.0;
			
			for (int i=0;i<lines.length;i++)
			{
				if (lines[i].length()<1) continue;
		//		System.out.println(lines[i]);
				String term=files[t].toString().replace(filepath+"cnet", "").replace(evaluatefile, "")+"\t"+lines[i].split("\t")[0]+"\t"+lines[i].split("\t")[1];
		//		System.out.println("term"+term);
				if (gth.containsKey(term))
				{
					Double s=gth.get(term)/i;
					if (i<=5) 	{p5=p5+1;dcg5=dcg5+s;}
					if (i<=1)	{p1=p1+1;dcg1=dcg1+s; }
					if (i<=2)	{p2=p2+1;dcg2=dcg2+s; }
					if (i<=3)	{p3=p3+1;dcg3=dcg3+s; }
					if (i<=4)	{p4=p4+1;dcg4=dcg4+s;}			
				}
			}
			String companyname=files[t].toString().replace(filepath+"cnet", "").replace(evaluatefile, "");
			Integer productnum=companyallproducts.get(companyname);
			if (companyallproducts.containsKey(companyname))
			{
				System.out.println(companyname+
						"\t"+p1/1+"\t"+p2/2+"\t"+p3/3+"\t"+p4/4+"\t"+p5/5+
						"\t\t"+p1/productnum+"\t"+p2/productnum+"\t"+p3/productnum+"\t"+p4/productnum+"\t"+p5/productnum+
						"\t\t"+dcg1+"\t"+dcg2+"\t"+dcg3+"\t"+dcg4+"\t"+dcg5);
			}
			
			allp5=allp5+p5/5;allp1=allp1+p1/1;allp2=allp2+p2/2;allp3=allp3+p3/3;allp4=allp4+p4/4;
					
			alldcg5=alldcg5+dcg5;alldcg1=alldcg1+dcg1;alldcg2=alldcg2+dcg2;alldcg3=alldcg3+dcg3;alldcg4=alldcg4+dcg4;
			allr5=allr5+p5/productnum;allr1=allr1+p1/productnum;allr2=allr2+p2/productnum;allr3=allr3+p3/productnum;allr4=allr4+p4/productnum;
		}
		}
		avgp5=allp5/20;avgp1=allp1/20;avgp2=allp2/20;avgp3=allp3/20;avgp4=allp4/20;
				
		avgr5=allr5/20;avgr1=allr1/20;avgr2=allr2/20;avgr3=allr3/20;avgr4=allr4/20;
			
		avgdcg5=alldcg5/20;avgdcg1=alldcg1/20;avgdcg2=alldcg2/20;avgdcg3=alldcg3/20;avgdcg4=alldcg4/20;
				
//		System.out.println("content \t\t 5 \t 10 \t 20\t30\t40\t50\t60\t70\t80\t90\t100\t\t5 \t 10 \t 20\t30\t40\t50\t60\t70\t80\t90\t100\t\t5 \t 10 \t 20\t30\t40\t50\t60\t70\t80\t90\t100\t");
//		System.out.println("sum up all precisions"+"\t\t"+allp5+"\t"+allp10+"\t"+allp20+"\t"+allp30+"\t"+allp40+"\t"+allp50+"\t"+allp60+"\t"+allp70+"\t"+allp80+"\t"+allp90+"\t"+allp100+"\t"+
//				"sum up all recalls"+"\t"+allr5+"\t"+allr10+"\t"+allr20+"\t"+allr30+"\t"+allr40+"\t"+allr50+"\t"+allr60+"\t"+allr70+"\t"+allr80+"\t"+allr90+"\t"+allr100+"\t"+
//				"sum up all DCGs"+"\t"+alldcg5+"\t"+alldcg10+"\t"+alldcg20+"\t"+alldcg30+"\t"+alldcg40+"\t"+alldcg50+"\t"+alldcg60+"\t"+alldcg70+"\t"+alldcg80+"\t"+alldcg90+"\t"+alldcg100);
				
		System.out.println("average overall precisions"+"\t"+avgp1+"\t"+avgp2+"\t"+avgp3+"\t"+avgp4+"\t"+avgp5+"\t"+
				"average overall recalls"+"\t"+avgr1+"\t"+avgr2+"\t"+avgr3+"\t"+avgr4+"\t"+avgr5+"\t"+
				"average overall DCGs"+"\t"+avgdcg1+"\t"+avgdcg2+"\t"+avgdcg3+"\t"+avgdcg4+"\t"+avgdcg5);

	}
}
